package com.chaos.auth.controller;

import com.chaos.auth.api.data.OauthClientDetailsPermissionData;
import com.chaos.auth.api.dto.auth.OauthClientDetailsPermissionDto;
import com.chaos.auth.entity.OauthClientDetailsPermission;
import com.chaos.auth.service.OauthClientDetailsPermissionService;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.dto.result.data.BooleanResult;
import com.chaos.framework.model.dto.result.data.DataResult;
import com.chaos.framework.model.dto.result.list.ListResult;
import com.chaos.framework.model.dto.result.page.PageResult;
import com.chaos.framework.starter.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jacky.Cui
 * @since 2024-11-21
 */
@Slf4j
@RestController
@Api(tags = "客户端权限表")
@RequestMapping("/oauthClientDetailsPermission")
public class OauthClientDetailsPermissionController extends BaseController<OauthClientDetailsPermissionData> {
    @Resource
    private OauthClientDetailsPermissionService oauthClientDetailsPermissionService;

    @PostMapping("/one.admin")
    @ApiOperation(value = "客户端权限表详情", httpMethod = "POST")
    public DataResult<OauthClientDetailsPermissionData> one(@RequestBody ID data) {
        return dataResult(oauthClientDetailsPermissionService.selectByID(data));
    }

    @PostMapping("/list.admin")
    @ApiOperation(value = "客户端权限表列表", httpMethod = "POST")
    public ListResult<OauthClientDetailsPermissionData> list(@RequestBody OauthClientDetailsPermissionData data) {
        return listResult(oauthClientDetailsPermissionService.selectByData(data));
    }

    @PostMapping("/page.admin")
    @ApiOperation(value = "客户端权限表分页", httpMethod = "POST")
    public PageResult<OauthClientDetailsPermissionData> page(@RequestBody PageQueryDto<OauthClientDetailsPermissionData> data) {
        return pageResult(oauthClientDetailsPermissionService.selectByPage(data));
    }

    @PostMapping("/add.admin")
    @ApiOperation(value = "新增客户端权限表", httpMethod = "POST")
    public BooleanResult add(@RequestBody OauthClientDetailsPermissionDto data) {
        List<OauthClientDetailsPermission> list = data.getPermissionIds().stream().map(
                        permissionId -> OauthClientDetailsPermission
                                .builder()
                                .clientId(data.getClientId())
                                .permissionId(permissionId)
                                .build())
                .collect(Collectors.toList());
        return booleanResult(oauthClientDetailsPermissionService.saveBatch(list));
    }

    @PostMapping("/update.admin")
    @ApiOperation(value = "编辑客户端权限表", httpMethod = "POST")
    public BooleanResult update(@RequestBody OauthClientDetailsPermissionData data) {

        return booleanResult(oauthClientDetailsPermissionService.updateModelByID(data));
    }

    @PostMapping("/delete.admin")
    @ApiOperation(value = "删除客户端权限表", httpMethod = "POST")
    public BooleanResult delete(@RequestBody ID data) {
        return booleanResult(oauthClientDetailsPermissionService.deleteModel(data));
    }

}
